﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLWrapper;
using GameBaseCode;

namespace sph
{
    class Program
    {
        public static int xres = 1024;
        public static int yres = 768;

        static void Main(string[] args)
        {
            GL.glfwInit();
            GL.glfwOpenWindow(xres,yres, 8, 8, 8, 0, 24, 0, GL.GLFW_WINDOW);
            GL.glfwSetWindowTitle("OpenGL For Fun & Profit".ToCharArray());

            GL.glfwSwapInterval(1);

            int fps = 0;
            double oneSec = GL.glfwGetTime();
            int frames = 0;

            int mousx, mousy;
            float camx = 0;
            float camy = 5;
            float camz = 5;
            while (GL.glfwGetWindowParam(GL.GLFW_OPENED) == 1)
            {
                double time = GL.glfwGetTime();
                unsafe
                {
                    GL.glfwGetMousePos(&mousx, &mousy);
                }

                GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

                GL.glMatrixMode(GL.GL_PROJECTION);
                GL.glLoadIdentity();
                GL.glusPerspective(60.0f, 4.0f / 3.0f, 0.01f, 100.0f);

                GL.glEnable(GL.GL_DEPTH_TEST);

                float dx = GL.glfwGetKey('D') - GL.glfwGetKey('A');
                float dz = GL.glfwGetKey('S') - GL.glfwGetKey('W');
                camx += dx * 0.5f;
                camz += dz * 0.5f;

                GL.glTranslatef(-camx,-camy,-camz);

                GL.glPushMatrix();
                GL.glRotatef( (float)time * 100.0f, 0, 1, 0);

#region TRI
                 GL.glBegin(GL.GL_TRIANGLES);  
                    GL.glColor3f(1.0f, 0.0f, 0.0f);
                    GL.glVertex3f(-1, -1, 0);

                    GL.glColor3f(0.0f, 1.0f, 0.0f);
                    GL.glVertex3f(1, -1, 0);

                    GL.glColor3f(0.0f, 0.0f, 1.0f);
                    GL.glVertex3f(0, 1, 0);
                GL.glEnd();
#endregion

                GL.glPopMatrix();

#region DRAWQUAD
                float size = 5;
                GL.glBegin(GL.GL_QUADS);
                GL.glColor3f(1.0f, 0.0f, 0.0f);
                GL.glVertex3f(-size, 0, -size);

                GL.glColor3f(1.0f, 0.0f, 0.0f);
                GL.glVertex3f(size, 0, -size);

                GL.glColor3f(0.0f, 1.0f, 0.0f);
                GL.glVertex3f(size, 0, size);

                GL.glColor3f(1.0f, 0.0f, 1.0f);
                GL.glVertex3f(-size, 0, size);
                GL.glEnd();
#endregion

                GL.glMatrixMode(GL.GL_PROJECTION);
                GL.glLoadIdentity();
                GL.glOrtho(0, xres, yres, 0, -1, 1);         
                
                GL.glBegin(GL.GL_POINTS);
                for (int i = 0; i < 64; i++ )
                    GL.glVertex3f(mousx + Util.nrnd() * 6, mousy+Util.nrnd() * 6, 0.0f );
                GL.glEnd();

                
                if (time > oneSec)
                {
                    fps = frames;
                    frames = 0;
                    oneSec = time + 1.0;
                }
                GL.glColor3f(1, 1, 1);
                Bitmapfont.printAt("fps: " + fps, 10, 10);

                frames++;
                GL.glGetError();
                GL.glfwSwapBuffers();
            }


            GL.glfwTerminate();
        
        } // main
    } // class
} // namespace
